#!/usr/bin/php
<?php
$connect = mysql_connect("localhost","beerwatch","beerwatch") or die(mysql_error());
$db = mysql_select_db("beerwatch",$connect);
$file=$argv[0];
$ti = explode("_",$file);
$temp_index=$ti[1];

if ($argc == 2) {
	if ($argv[1] == "config") {
		echo "graph_title Temp for index: $temp_index\n";
		echo "graph_vlabel Temp Index\n";
		echo "graph_category Beerwatch\n";
		echo "graph_info A visulization of the temp\n";
		echo "temp.label Temp for index: $temp_index\n";
	}
	exit;
}

$sql = "select * from last_readings where temp_index=\"$temp_index\" order by readingtime desc limit 1";
$r = mysql_query($sql,$connect);
$row = mysql_fetch_array($r);
$d = $row[0];
$sql = "select round(AVG(temp),2) from currenttemps where temp_index=\"$temp_index\" and readingtime > \"$d\"";
$r = mysql_query($sql,$connect);
$row = mysql_fetch_row($r);
$sql = "INSERT into last_readings(temp_index,readingtime) values (\"$temp_index\",now())";
$r = mysql_query($sql,$connect);
echo "temp.value " . $row[0] ."\n";
?>
